package com.wf.smartcomm.server.serviceImpl;


import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.wf.smartcomm.server.bean.Village;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.VillageService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-26 下午12:00:21 
 * 
 */
@Service
@Transactional
public class VillageServiceImpl implements VillageService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Village village) {
		village.setObjectId(IdUtils.uuid());
		village.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		village.setUpdateAt(village.getCreatedAt());
		factory.getCurrentSession().persist(village);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void list(Page page) {
		Query query = factory.getCurrentSession().createQuery("from Village order by updateAt desc");
		page.setDataList(query.list());
	}
	
	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listCity(Page page) {
		Query query = factory.getCurrentSession().createSQLQuery("select distinct city from t_village");
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listByCity(String city,Page page) {
		Query query = factory.getCurrentSession().createQuery("from Village where city = :city");
		query.setParameter("city", city);
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void listToAdmin(Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Village");
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Village order by updateAt desc");
		query.setFirstResult(page.getBeginIndex());
		query.setMaxResults(page.getPageSize());
		page.setDataList(query.list());
	}

	@Override
	public void update(Village village) {
		village.setUpdateAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().update(village);
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Village get(String objectId) {
		Query query = factory.getCurrentSession().createQuery("from Village where objectId =:id");
		query.setParameter("id", objectId);
		return (Village) query.uniqueResult();
	}

	@Override
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public void queryByKey(String key, Page page) {
		Query query = factory.getCurrentSession().createQuery("from Village where name like :key");
		query.setParameter("key", "%"+key+"%");
		page.setDataList(query.list());
	}

	@Override
	public Village getByName(String villageName) {
		Query query = factory.getCurrentSession().createQuery("from Village where name =:name");
		query.setParameter("name", villageName);
		return (Village) query.uniqueResult();
	}
}
